const {
  comMysql
} = require("../db/mysql")
const xss = require("xss")
const fs = require("fs")

const Prod = (req, res) => {
  let {
    prod,
    username
  } = req.query
  prod = "http://localhost:3001/api/upload/showMp4?prod=" + prod + "&username=" + username
  let sql = "select * from prod where username = ? and prod = ?"
  console.log(prod)
  let arr = [username, prod]
  return comMysql(sql, arr)
}
const Poster = (req, res) => {
  let {
    poster,
    username
  } = req.query
  poster = "http://www.ttzxh.icu/api/upload/showMp4?poster=" + poster + "&username=" + username
  let sql = "select * from prod where username = ? and poster = ?"
  console.log(poster)

  let arr = [username, poster]
  return comMysql(sql, arr)
}

const addProd = (req, res) => {
  let {
    username,
    way,
    prod,
    introduce,
    title,
    text,
    poster,
    render,
    format
  } = req.body
  title = xss(title)
  introduce = xss(introduce)
  text = xss(text)
  render = xss(render)
  let time = new Date().getTime()
  console.log(username,
    way,
    prod,
    introduce,
    title,
    text,
    poster,
    format,
    render)
  let sql = `insert into prod(username,way,prod,introduce,time,title,text,poster,render,format) values (?,?,?,?,?,?,?,?,?,?)`
  let arr = [username, way, prod, introduce, time, title, text, poster,render,format]
  return comMysql(sql, arr)
}

const delProd = async(req, res) => {
  let {
    username,
    id,
  } = req.body
  let sql = `delete from prod where username = ? and id = ?`
  let arr = [username, id]
  //在更新之前删除之前的文件
  let data =await searchProdForDel(req,res)
    if(data.length>0&&data[0].prod){
      let num = data[0].prod.indexOf("?")      
      let next1=data[0].prod.substring(num)
      let next2=next1.split("&")[0].split("=")[1]
      let next3 = "./"+next2
      if(fs.existsSync(next3)){
        fs.unlink(next3,(err)=>{
          if(!err){
            console.log("删除成功");
          }else{
            console.log("删除失败");
            
          }
        })
      }
    }
    if(data.length>0&&data[0].poster){
      let num = data[0].poster.indexOf("?")      
      let next1=data[0].poster.substring(num)
      let next2=next1.split("&")[0].split("=")[1]
      let next3 = "./"+next2
      if(fs.existsSync(next3)){
        fs.unlink(next3,(err)=>{
          if(!err){
            console.log("删除成功");
          }else{
            console.log("删除失败");
            
          }
        })
      }
    }
  return comMysql(sql, arr)
}

const updateProd =async (req, res) => {
  let {
    username,
    id,
    introduce,
    prod,
    title,
    text,
    poster,
    render
  } = req.body
  introduce = xss(introduce)
  title = xss(title)
  text = xss(text)
  render = xss(render)
  let moreUpdate = ``
  let arr = [title, introduce, username, id]
  if (prod) {
    moreUpdate += ` ,prod = ? `
    arr.splice(2, 0, prod)
  }
  if (text&&render) {
    moreUpdate += ` ,text = ?,render = ? `
    arr.splice(2, 0, text, render)
  }
  if(poster){
    moreUpdate += ` ,poster = ? `
    arr.splice(-2, 0 ,poster)
  }
  //在更新之前删除之前的文件
  let data =await searchProdForDel(req,res)
    if(data.length>0&&data[0].prod){
      let num = data[0].prod.indexOf("?")      
      let next1=data[0].prod.substring(num)
      let next2=next1.split("&")[0].split("=")[1]
      let next3 = "./"+next2
      if(fs.existsSync(next3)){
        fs.unlink(next3,(err)=>{
          if(!err){
            console.log("删除成功");
          }else{
            console.log("删除失败");
            
          }
        })
      }
    }
    if(data.length>0&&data[0].poster){
      let num = data[0].poster.indexOf("?")      
      let next1=data[0].poster.substring(num)
      let next2=next1.split("&")[0].split("=")[1]
      let next3 = "./"+next2
      if(fs.existsSync(next3)){
        fs.unlink(next3,(err)=>{
          if(!err){
            console.log("删除成功");
          }else{
            console.log("删除失败");
            
          }
        })
      }
    }
  let sql = `update prod set title = ? , introduce = ?  ` + moreUpdate + ` where  username = ? and id = ?`
  

  return comMysql(sql, arr)
}

const searchProd = (req, res) => {
  let sql = `select users.realname,users.avatar,prod.id,prod.username,prod.prod,prod.time,prod.way,
  prod.introduce,prod.title,prod.text,prod.render,prod.poster from prod left join users on prod.username=users.username order by prod.time desc`
  return comMysql(sql)
}

const UserSearchProd = (req, res) => {
  let {searchTitle} = req.query
  searchTitle = xss(searchTitle)
    
  let sql = `select users.realname,users.avatar,prod.id,prod.username,prod.prod,prod.time,prod.way,
  prod.introduce,prod.title,prod.text,prod.render,prod.poster from prod left join users on prod.username=users.username where 
  prod.title like "%${searchTitle}%" order by prod.time desc`
  return comMysql(sql)
}

const searchProdForDel = (req,res)=>{
  let {id} = req.body
  let sql = `select * from prod where id = ?`
  let arr = [id]
  return comMysql(sql,arr)
}

module.exports = {
  Prod,
  addProd,
  updateProd,
  searchProd,
  delProd,
  Poster,
  UserSearchProd,
  searchProdForDel
}